package com.offcn.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.Dao.UserDao;
import com.offcn.bean.ResultVO;
import com.offcn.bean.UserBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    //声明一个dao的对象
    private UserDao userDao = new UserDao();
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //2:接受数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //调用dao层
        UserBean user = userDao.login(username, password);
        //3:给出响应
        ResultVO vo=null;
        if (user!=null){
            vo=new ResultVO(200,"登录成功",user);
        }else{
            vo=new ResultVO(500,"登录失败",null);
        }
        //将vo转为json对象
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(vo);
        resp.getWriter().print(json);

    }
//进行安全退出
private void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //进行销毁session
    HttpSession session = req.getSession();
    session.invalidate();//解绑session中的所有对象
    //重定向到login.jsp页面
    resp.sendRedirect("login.jsp");
    }
}